package com.ltmall.api.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class DigestUtil {

    public static boolean interfaceVerify(String params, String action) throws UnsupportedEncodingException {
        System.out.println("params:" + params);
        params = URLDecoder.decode(params, "UTF-8");
        String[] paramsArr = params.split("&");
        Arrays.sort(paramsArr);

        String s1 = "";
        String s2 = "";
        for (String key : paramsArr) {
            System.out.println("key: " + key);
            if (key.contains("s1=")) {
                s1 = key;
                System.out.println("s1: " + s1);
            } else if (key.contains("s2=")) {
                s2 = key;
                System.out.println("s2: " + s2);
            }
        }

        if (s1 == "" || s2 == "") {
            System.out.println("s1 or s2 is empty");
            return false;
        }

        List<String> list = new ArrayList<>(Arrays.asList(paramsArr));
        list.remove(s1.toString());
        list.remove(s2.toString());
        paramsArr = list.toArray(new String[0]);

        params = paramsArr[0];
        for (int i = 1; i < paramsArr.length; i++) {
            params += "&" + paramsArr[i];
        }
        System.out.println("params: " + params);
        s1 = s1.substring(3);
        s2 = s2.substring(3);

        String[] s1Arr = s1.split("");
        Arrays.sort(s1Arr);
        for (String key : s1Arr) {
            System.out.println("key: " + key);
        }
        s1 = Arrays.toString(s1Arr);
        System.out.println("s1: " + s1);

        Arrays.sort(s1Arr);
        String s3 = "";
        for (int i = 0; i < 10; i++) {
            s3 += s1Arr[i];
        }
        System.out.println("s3: " + s3);

        String s2Ver = DigestUtil.Md5(params + DigestUtil.Md5(action + s3));
        System.out.println("Md5(action+s3): " + DigestUtil.Md5(action + s3));
        System.out.println("s2Ver: " + s2Ver);

        if (!s2Ver.equals(s2)) {
            System.out.println("s2 not matched!");
            return false;
        }

        return true;
    }

    public static String Md5(String str) throws UnsupportedEncodingException {
        MessageDigest md;
        try {
            byte[] plainText = (str).getBytes("UTF-8");
            md = MessageDigest.getInstance("MD5");
            byte[] digest = md.digest(plainText);

            StringBuffer buf = new StringBuffer("");
            for (int i = 0; i < digest.length; i++) {
                if (Integer.toHexString(0xFF & digest[i]).length() == 1) {
                    buf.append("0").append(Integer.toHexString(0xFF & digest[i]));
                } else {
                    buf.append(Integer.toHexString(0xFF & digest[i]));
                }
            }

            return buf.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        // TODO Auto-generated method stub
        String str = "2e94256a441df4495587654df5c8bc11";
        System.out.println("md5: " + DigestUtil.Md5(str));
    }
}
